//-------------------------------------------------------------------
//	Copyright (c) 2010-2012 Zhirnov Andrey
//	This file is part of the "UXGEN" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#ifndef _UX_RESOURCE_H
#define _UX_RESOURCE_H

#include <Includes.h>
#include "../../Interfaces.h"

namespace Engine
{
namespace Resources
{

	//
	// Resource
	//

	class _EXPORT_ CResource : public IResource
	{
		friend class CResourceManager;

	protected:
		IResourceLoader	*		_pLoader;
		usize					_uRAMSize,			// resource data size in RAM
								_uVRAMSize;			// resource data size in VideoRAM
		e_res_group::type		_eGroupType;		// resource group type
		bool					_bLocked;			// lock resource data after creating/loading

	protected:
		// IResource //
		virtual void _SetLoader(IResourceLoader *pLoader)		{ _pLoader = pLoader; }
		virtual void _SetGroup(e_res_group::type eType)			{ _eGroupType = eType; }

	public:
		CResource(): _uRAMSize(0), _uVRAMSize(0), _eGroupType(e_res_group::UNKNOWN), _bLocked(false) {}
		
		// IResource //
		virtual bool Compare(const IResourcePtr &)				{ return false; }
		virtual bool Reload(bool bReloadDependings)				{ CHECK_VALUE( _pLoader != nullptr );  CHECK_RETURN( _pLoader->Reload( this ) );  return true; }
		virtual void GetMemoryInfo(usize &uRAM, usize &uVRAM)	{ uRAM = _uRAMSize;  uVRAM = _uVRAMSize; }
		virtual IResourceLoader *	GetLoader()			const	{ return _pLoader; }
		virtual e_res_group::type	GetGroupType()		const	{ return _eGroupType; }
		virtual bool				IsLocked()			const	{ return _bLocked; }
		virtual bool				SwapData(const IResourcePtr &);
		
		// override CEngBaseObject //
		virtual void Release()		{ WARNING("!!!"); }
	};


	inline bool CResource::SwapData(const IResourcePtr &pResource)
	{
		CHECK_ARGUMENT( pResource != nullptr && pResource->GetType() == GetType() );
		
		CResource *	p_res = (CResource *)pResource.ptr();
		
		_swap( this->_pLoader,		p_res->_pLoader );
		_swap( this->_uRAMSize,		p_res->_uRAMSize );
		_swap( this->_uVRAMSize,		p_res->_uVRAMSize );
		_swap( this->_eGroupType,	p_res->_eGroupType );
		_swap( this->_bLocked,		p_res->_bLocked );
		return true;
	}

//-------------------------------------------------------------------

}	// Resources
}	// Engine

#endif	// _UX_RESOURCE_H